package com.ybear.ybhotrepair.utils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 计算校验值工具类
 */
public class DigestUtil {
    public static final String MD5 = "MD5";
    public static final String SHA1 = "SHA-1";
    public static final String SHA256 = "SHA-256";
    public static final String SHA384 = "SHA-384";
    public static final String SHA512 = "SHA-512";

    /**
     * 根据摘要类型计算校验值
     * @param data          计算的数据
     * @param algorithm     摘要类型
     * @return              校验值
     */
    @Nullable
    public static String toDigest(@NonNull byte[] data, @NonNull String algorithm) {
        try {
            MessageDigest digest = MessageDigest.getInstance( algorithm );
            digest.update( data );
            return toHexString( digest.digest() );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String toDigest(String data, String algorithm) {
        return toDigest(data.getBytes( Charset.forName("UTF-8") ), algorithm);
    }

    /**
     * 计算文件的校验值
     * @param filePath      文件路径
     * @param algorithm     摘要类型
     * @return              校验值
     */
    @Nullable
    public static String toFileDigest(@NonNull String filePath, @NonNull String algorithm) {
        InputStream is = null;
        byte[] bsData;
        try {
            is = new FileInputStream( filePath );
            bsData = new byte[ is.available() ];
            if( is.read( bsData ) == bsData.length ) return toDigest(bsData, algorithm);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( is != null ) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    public static String toFileDigest(@NonNull File file, @NonNull String algorithm) {
        return toFileDigest(file.getAbsolutePath(), algorithm);
    }

    public static String toMD5(byte[] data) {
        return toDigest(data, MD5);
    }
    public static String toSHA1(byte[] data) {
        return toDigest(data, SHA1);
    }
    public static String toSHA256(byte[] data) {
        return toDigest(data, SHA256);
    }
    public static String toSHA384(byte[] data) {
        return toDigest(data, SHA384);
    }
    public static String toSHA512(byte[] data) {
        return toDigest(data, SHA512);
    }

    public static String toMD5(String data) {
        return toDigest(data, MD5);
    }
    public static String toSHA1(String data) {
        return toDigest(data, SHA1);
    }
    public static String toSHA256(String data) {
        return toDigest(data, SHA256);
    }
    public static String toSHA384(String data) {
        return toDigest(data, SHA384);
    }
    public static String toSHA512(String data) {
        return toDigest(data, SHA512);
    }

    public static String toFileMD5(String filePath) {
        return toFileDigest(filePath, MD5);
    }
    public static String toFileSHA1(String filePath) {
        return toFileDigest(filePath, SHA1);
    }
    public static String toFileSHA256(String filePath) {
        return toFileDigest(filePath, SHA256);
    }
    public static String toFileSHA384(String filePath) {
        return toFileDigest(filePath, SHA384);
    }
    public static String toFileSHA512(String filePath) {
        return toFileDigest(filePath, SHA512);
    }

    public static String toFileMD5(File file) {
        return toFileDigest(file, MD5);
    }
    public static String toFileSHA1(File file) {
        return toFileDigest(file, SHA1);
    }
    public static String toFileSHA256(File file) {
        return toFileDigest(file, SHA256);
    }
    public static String toFileSHA384(File file) {
        return toFileDigest(file, SHA384);
    }
    public static String toFileSHA512(File file) {
        return toFileDigest(file, SHA512);
    }

    private static String toHexString(byte[] bs) {
        StringBuilder sb = new StringBuilder();
        String s;
        for( byte b : bs ) {
            s = Integer.toHexString(b & 0xFF);
            //1得到一位的进行补0操作
            if (s.length() == 1) sb.append("0");
            sb.append(s);
        }
        return sb.toString();
    }
}
